import MinRequest from "./request";

const minRequest = new MinRequest();

// 请求拦截器
minRequest.interceptors.request((request: any) => {
  request.header["x-access-token"] =
    uni.getStorageSync("TALLY_BOOK_ACCESS_TOKEN") ||
    uni.getStorageSync("TALLY_BOOK_WX_CODE");
  // request.header.userId = uni.getStorageSync('TALLY_BOOK_USER_ID');
  return request;
});

// 响应拦截器
minRequest.interceptors.response((response: any) => {
  if (response.statusCode === 200 && response.data.code === 1001) {
    uni.removeStorageSync("TALLY_BOOK_IS_LOGIN");
    uni.removeStorageSync("TALLY_BOOK_USER");
    uni.showToast({
      icon: "none",
      title: "登录失效,请重新登录",
    });
    uni.reLaunch({
      url: "/pages/personal/login",
    });
    // return response.data;
  }
  if (response.statusCode === 500) {
    uni.showToast({
      icon: "none",
      title: "网络错误，请稍后重试",
    });
  }
  if (
    response.statusCode === 200 &&
    response.data &&
    response.data.code !== 1
  ) {
    uni.showToast({
      icon: "none",
      title: response.data.msg,
    });
    return response.data;
  } else {
    return response.data;
  }
});

export default minRequest;
